﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VIA.SiMoJi.Domain
{
    public class ZKTEcoRequest
    {
        public string Method { get; set; }
        public string Url { get; set; }
        public string Body { get; set; }
        protected virtual void Build() { }
        public ZKTEcoRequest(string method, string uri, string body)
        {
            Method = method;
            Url = uri;
            Body = body;
            Build();
        }
    }
    /// <summary>
    /// 初始化请求
    /// </summary>
    public class InitializeRequest : ZKTEcoRequest
    {
        //--http://192.168.1.50:8081/iclock/cdata?SN=BEF5234400174&options=all&language=83&pushver=2.4.1&DeviceType=att&PushOptionsFlag=1
        /// <summary>
        /// 客户端的序列号
        /// </summary>
        public string SN { get; set; }
        /// <summary>
        /// 获取服务器配置参数，目前值只有all
        /// </summary>
        public string Options { get; set; }
        /// <summary>
        /// 语言
        /// </summary>
        public ZKTEcoLanguage Language { get; set; }
        /// <summary>
        /// 表示设备当前最新的push协议版本，
        /// </summary>
        public string Pushver { get; set; }
        /// <summary>
        /// 子协议版本，本协议支持对接标准考勤协议、人证协议、信息屏协议，
        /// </summary>
        public ZKTEcoDeviceType DeviceType { get; set; }
        /// <summary>
        /// 软件是否支持设备推送配置参数请求，0不支持，1支持，未设置时默认不支持
        /// </summary>
        public int PushOptionsFlag { get; set; }
        public InitializeRequest(ZKTEcoRequest request) : this(request.Method, request.Url, request.Body)
        {

        }
        public InitializeRequest(string method, string url, string body) : base(method, url, body)
        {
        }
        protected override void Build()
        {
            var uriArray = Url.Split(new char[] { '?', '&' });
            SN = uriArray[1].Replace("SN=", string.Empty);
            Options = uriArray[2].Replace("options=", string.Empty);
            SN = uriArray[2].Replace("language=", string.Empty);
            Pushver = uriArray[3].Replace("pushver=", string.Empty);
            DeviceType = Enum.Parse<ZKTEcoDeviceType>(uriArray[5].Replace("DeviceType=", string.Empty));
            PushOptionsFlag = Convert.ToInt32(uriArray[6].Replace("PushOptionsFlag=", string.Empty));
        }
    }
    /// <summary>
    /// 心跳请求
    /// </summary>
    public class HeartbeatRequest : ZKTEcoRequest
    {
        public string SN { get; set; }
        public HeartbeatRequest(ZKTEcoRequest request) : this(request.Method, request.Url, request.Body)
        {

        }
        public HeartbeatRequest(string method, string uri, string body) : base(method, uri, body)
        {
        }
        protected override void Build()
        {
            var uriArray = Url.Split(new char[] { '?', '&' });
            SN = uriArray[1].Replace("SN=", string.Empty);
        }
    }
    /// <summary>
    /// 操作记录
    /// </summary>
    public class OperatingRequest : ZKTEcoRequest
    {
        public ZKTEcoOperationType OperationType { get; set; }
        public DateTime DateTime { get; set; }
        public OperatingRequest(ZKTEcoRequest request) : this(request.Method, request.Url, request.Body)
        {

        }
        public OperatingRequest(string method, string uri, string body) : base(method, uri, body)
        {
        }
        protected override void Build()
        {
            var bodyArray = Body.Split('\t');
            OperationType = (ZKTEcoOperationType)Convert.ToInt32(bodyArray[0].Replace("OPLOG ", string.Empty));
            DateTime = DateTime.Parse(bodyArray[2]);
        }
    }
    /// <summary>
    /// 用户登录请求
    /// </summary>
    public class LoginRequest : ZKTEcoRequest
    {
        /// <summary>
        /// 账号用户
        /// </summary>
        public string Account { get; set; }
        /// <summary>
        /// 登录时间
        /// </summary>
        public DateTime DateTime { get; set; }
        /// <summary>
        /// 验证方式
        /// </summary>
        public ZKTEcoVerifyType VerifyType { get; set; }
        public LoginRequest(ZKTEcoRequest request) : this(request.Method, request.Url, request.Body)
        {

        }
        public LoginRequest(string method, string uri, string body) : base(method, uri, body)
        {
            string[] bodyArray = Body.Split('\t');
            Account = bodyArray[0];
            DateTime = DateTime.Parse(bodyArray[1]);
            VerifyType = (ZKTEcoVerifyType)(Convert.ToInt32(bodyArray[3]));
        }
    }
    /// <summary>
    /// 上传用户信息请求
    /// </summary>
    public class UploadUserInfoRequest : ZKTEcoRequest
    {
        /// <summary>
        /// 账号用户
        /// </summary>
        public string Account { get; set; }
        /// <summary>
        /// 用户名称
        /// </summary>
        public string UserName { get; set; }
        /// <summary>
        /// 卡号
        /// </summary>
        public string Crad { get; set; }
        /// <summary>
        /// 密码
        /// </summary>
        public string Password { get; set; }
        public UploadUserInfoRequest(ZKTEcoRequest request) : this(request.Method, request.Url, request.Body)
        {

        }
        public UploadUserInfoRequest(string method, string uri, string body) : base(method, uri, body)
        {
        }
        protected override void Build()
        {
            string[] bodyArray = Body.Split('\t');
            Account = bodyArray[0].Replace("USER PIN=", string.Empty);
            UserName = bodyArray[1].Replace("Name=", string.Empty);
            Crad = bodyArray[3].Replace("tCard=", string.Empty);
            Password = bodyArray[4].Replace("tPasswd=", string.Empty);
        }
    }
}
